package com.neuedu.controller;


import com.neuedu.pojo.Result;
import com.neuedu.pojo.TElder;
import com.neuedu.pojo.TRecord;
import com.neuedu.pojo.dto.nurse.NurseInfoDTO;
import com.neuedu.pojo.dto.record.CheckIOPageDTO;
import com.neuedu.pojo.dto.record.CheckIOPageResult;
import com.neuedu.pojo.dto.record.DeleteRecordDTO;
import com.neuedu.pojo.dto.record.RecordEditDTO;
import com.neuedu.service.ITElderService;
import com.neuedu.service.ITRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhangsan
 * @since 2024-06-17
 */
@RestController
@RequestMapping("/t-record")
public class TRecordController {
    @Autowired
    private ITRecordService recordService;
    @Autowired
    private ITElderService elderService;

    //分页
    @GetMapping("/checkIO/page")
    public Result<List<CheckIOPageResult>> getPage(CheckIOPageDTO dto) {
        return recordService.getPage(dto);
    }


    //删除
    @PostMapping("/checkIO/del")
    public Result del(@RequestBody DeleteRecordDTO dto){
        recordService.removeById(dto.getRecordId());
        return Result.success("删除成功");
    }

    //修改
    @PostMapping("/checkIO/edit")
    @Transactional
    public Result edit(@RequestBody RecordEditDTO dto){
        TRecord tRecord = new TRecord();
        BeanUtils.copyProperties(dto,tRecord);
        tRecord.setRecordTime(LocalDateTime.now());
        tRecord.setUpdateTime(LocalDateTime.now());
        recordService.updateById(tRecord);
        TElder tElder = new TElder();
        tElder.setEldId(dto.getElderId());
        tElder.setUpdateTime(LocalDateTime.now());
        if ( "入住登记".equals(tRecord.getTypeofrecord()) ) {
            switch (tRecord.getRecordState()){
                case 0:
                case 1:
                    tElder.setEldState(1);
                    break;
                case 2:
                    tElder.setEldState(0);
                    break;
            }
        }else {
            switch (tRecord.getRecordState()){
                case 0:
                case 1:
                    tElder.setEldState(0);
                    break;
                case 2:
                    tElder.setEldState(1);
                    break;
            }
        }

        elderService.updateById(tElder);
        return Result.success("修改成功");
    }
}
